<?php
namespace Admin\Controller;

use Admin\Util\lib\YZTokenClient;
use Util\AccessController;
use Admin\Util\lib\YouZanApiFunction;

class YouZanController extends AccessController {

    public function AddAllYZGoodsIndex() {
        if(!$this->S('access_token')){
            $this->redirect('Login/YZ_index');
        }
        $ALLDATA = S('EIdata');
        $this->DATA = $ALLDATA;
        $this->counts = count($ALLDATA);
        $this->display();
    }

    public function import() {

        if (!isset($_FILES['execel']) || $_FILES["import"]["error"] != 0) {
            exit('File Not Exit!');
        }
        $extension = explode('.', $_FILES['execel']['name']);
        $res = \Admin\Util\ExcelUtil::importExecl($_FILES['execel']['tmp_name'], $extension[1]);

        S('EIdata', $res);
        $this->redirect('YouZan/importHandle');
    }

    public function importHandle() {
        $res = S('EIdata');
        unset($res[0]);
        foreach ($res as $key => $value) {
            $data['title'] = $value[0];
            $data["price"] = $value[1];
            $data['post_fee'] = $value[2];
            $data['image_ids'] = $value[3];
            $data['desc'] =$value[4];
            $data['item_no'] = $value[5];
            $res[$key]['data'] = $data;
            $res[$key]['STATE'] = 1;
        }
        S('EIdata', $res);
        $this->redirect('YouZan/AddAllYZGoodsIndex');
    }

    public function clear() {
        S('EIdata', NULL);
        $this->redirect('YouZan/AddAllYZGoodsIndex');
    }

    public function importYZ() {
        $ALLDATA = S('EIdata');
        if (!$ALLDATA) {
            $this->error('请先将数据导入内存');
        }
        //未导入
        $id = $_POST['id']+1;
        if ($ALLDATA[$id]['STATE'] == 1) {
            $client = new YZTokenClient($this->S('access_token'));
            $method = 'youzan.item.create';//'kdt.item.add';//要调用的api名称
            $methodVersion ='3.0.0';//要调用的api版本号
            $RESDATA = $client->post($method, $methodVersion, $ALLDATA[$id]['data'], array());
            if ($RESDATA['response']) {
                $ALLDATA[$id]['STATE'] = 0;
                $ALLDATA[$id]['msg'] = 'OK';
            } else {
                $ALLDATA[$id]['STATE'] = -1;
                $ALLDATA[$id]['msg'] = $RESDATA['error_response']['msg'];
            }
        }
        S('EIdata', $ALLDATA);
        echo 1;
    }

    /*
     * 有赞图片上传
     */
    public function YZuploadIndex(){
        $this->display();
    }

    /*
     * 有赞图片上传处理
     */
    public function YZupload(){
      /*
         dump($_POST);
        dump($_FILES);
        dump( fopen($_FILES['image']['tmp_name'],"ab"));
        dump(file_get_contents($_FILES['image']['tmp_name']));
        die();
      */
        $image =  file_get_contents($_POST['url']);
        $client = new YZTokenClient($this->S('access_token'));
        $method = 'youzan.materials.storage.platform.img.upload';//'kdt.item.add';//要调用的api名称
        $methodVersion ='3.0.0';//要调用的api版本号
        $my_params = [
        ];
        $my_files = [
            [
                'url' => __DIR__ .'file.jpg',
                'field' => $image,
            ],
        ];
        $RESDATA = $client->post($method, $methodVersion, $my_params,$my_files);
        var_dump($RESDATA);
    }


}